<?php

namespace application\admin\panels;
use application\utilities\Utility;
use application\models\Box;


class mobileTemplates extends panel {
	public $name = 'Mobile Templates';
	
	static function templateSelect($id, $default = FALSE) {
		if ($handle = opendir(CN_DOCUMENT_ROOT . 'templates/')) {
		?>
		<select name="<?=$id?>">
			<option value="">No Custom Template</option>
		<?php
	    	while (false !== ($file = readdir($handle))) {
	       		if(is_dir(CN_DOCUMENT_ROOT . 'templates/'.$file) && $file != '.' && $file != '..') {
	       			if(file_exists(CN_DOCUMENT_ROOT . 'templates/'.$file.'/info.ini')) {
	       				$ini = parse_ini_file(CN_DOCUMENT_ROOT . 'templates/'.$file.'/info.ini');
	       				$title = $ini['Title'];
	       				?><option <?=($default == $file) ? 'selected="selected"' : ''?> value="<?=$file?>"><?=$title?></option><?php
	       			}
	       		}
	    	}
			
	    	closedir($handle);
		}
		?></select><?php
	}
	
	public function handleData() {
		if($_REQUEST['mobile_templates_update']) {
			setOption('custom_template_iphone', $_REQUEST['iphone_template']);
			setOption('custom_template_blackberry', $_REQUEST['blackberry_template']);
			setOption('custom_template_android', $_REQUEST['android_template']);
			$this->addMessage('success', 'Templates set successfully.');
		}
	}
	
	public static function display() {
		$this->displayMessages();
		?>
		<div class="one-column">
			<div class="box">
				<h3>Custom Mobile Templates</h3>
				<div class="box-content">
					<form method="post">
						<h4>iPhone: <?php self::templateSelect('iphone_template', getOption('custom_template_iphone'));?></h4>
						<h4>Blackberry: <?php self::templateSelect('blackberry_template', getOption('custom_template_blackberry'));?></h4>
						<h4>Android: <?php self::templateSelect('android_template', getOption('custom_template_android'));?></h4>
						
						<input type="submit" value="Update Settings" class="small-button" name="mobile_templates_update"/>
						
					</form>
				</div>
			</div>
		</div>
	
		<?php
	}
}

?>